/**
 * Created by xingzhe on 17/7/7.
 */
import React, { PropTypes } from 'react'
import API_LIST from '../config/apiList'

import { message } from 'antd';

import $ from 'jquery'


class Module extends React.Component {
    constructor(props, context) {
        super(props, context);
        this.context.router;
    }

    static contextTypes = {
        router: PropTypes.object.isRequired
    };

    request(options, success, fail) {
        if(options.api !== undefined) {
            let api = API_LIST[options.api];
            
            if(!api) {
                console.log('error', 'api 不存在', options.api);
                return;
            }

            $.ajax({
                "url": api,
                "type": options.type || "post",
                "ContentType": options.ContentType || "application/x-www-form-urlencoded; charset=UTF-8",
                "dataType": options.dataType || "json",
                "data": options.params || ""
            })
            .done(function (result) {
                if (result.result == '800') {
                    window.location.href = '//f.superboss.cc/static/newNoSession.html';
                    return false;
                }
                success && success(result);
            })
            .fail(function (errorThrown) {
                message.error('出错了!请重试或者联系客服!', 1);
            });
            
        }
    }

    /**
     *
     *  利用递归返回一个深度克隆的对象
     * @param {Object} data 需要被clone 的元素
     */
    deepClone(data) {
        let _data;
        switch(typeof data) {
            case 'string':
                _data = data + '';
                break;
            case 'number':
                _data = + data;
                break;
            case 'boolean':
                _data = !!data;
                break;
            default:
            case 'object':
                _data = this._clone(data);
                break;
        }
        return _data;
    }

    /**
     *
     *  递归克隆
     * @private
     * @param {Object} data
     * @param {Boolean} isFormatData        是否需要格式化
     */
    _clone(data, isFormatData) {
        var isArray = data instanceof Array,
            o = isArray ? [] : {},
            i, ni, it, itType;
        if (isArray) {
            for (i = 0, ni = data.length; i < ni; i++) {
                it = data[i];
                itType = typeof it;
                o.push(itType === 'object' ?
                    this._clone(it, isFormatData) : it);
            }
        } else if (data === null) {
            o = null;
        } else {
            for (i in data) {
                it = data[i];
                itType = typeof it;
                o[isFormatData ? '"' + i + '"' : i] = itType === 'object' ?
                    this._clone(it, isFormatData) : it;
            }
        }
        return o;
    }

    getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)){
            return unescape(arr[2]);
        } else {
            return null;
        }
    }
    

}

module.exports = Module;